const pool = require('../config/db');

class FarmerProductList {
  static tableName = 'farmer_product_list';

  static async create(farmerId, villageId, isListed,name) {
    const [result] = await pool.execute(
      `INSERT INTO ${this.tableName} (farmer_id, village_id, is_listed,name) VALUES (?, ?, ?, ?)`,
      [farmerId, villageId, isListed, name]
    );
    return result.insertId;
  }

  static async getByFarmerId(farmerId) {
    const [rows] = await pool.execute(
      `SELECT * FROM ${this.tableName} WHERE farmer_id = ?`,
      [farmerId]
    );
    return rows;
  }

  static async updateIsListed(id, isListed) {
    const [result] = await pool.execute(
      `UPDATE ${this.tableName} SET is_listed = ? WHERE id = ?`,
      [isListed, id]
    );
    return result.affectedRows;
  }
}

module.exports = FarmerProductList;